
import { Base } from './base.js'
import { china_city } from '../database/china_city.js'
//搜索类
class Search extends Base {

  //-----搜索操作部分-----

  //检查历史搜索是否有重复项
  checkHas(text='', array=[]){
   if( array.includes( text ) ) return false
   else return true;
  }

  //设置历史搜索数组进入缓存 超过截取
  setHistory(array){
   if( array.length > 16 ) array.pop();
   this.setLocal('history',array);
  }

  //清空历史搜索
  clearHistory(){
   wx.removeStorageSync('history')
  }

  //根据城市对象 获取市区对象
  getDistrict(city={}){
    const districts = [];
    for( let i of china_city ){
      if( i.parent == city.value ){
        districts.push( i )
      }
    }
    return districts; 
  }
  //是否有下级行政区
  hasDistrict(city){
    const arr = this.getDistrict(city);
    if( !arr.length || arr.length == 1 ) return false
    else return true
  }
  //-----网络请求部分-----
  
  //服务端 查询信息
  queryInfo({keywords='', sort='', city='',district=""}){
    return this.http( 'api/positions', { keywords, sort, city,district } )
  }
}

const searchClass = new Search();

export { searchClass }